package com.gpsnav.ui;

import net.rim.device.api.ui.*;
import net.rim.device.api.ui.container.*;
import net.rim.device.api.ui.component.*;


public class ProgressBar {
  private PopupScreen popup;
  private GaugeField gaugeField;
  private LabelField label;
//  private CustomButtonField cancelButton;

//  private ProgressStatus monitor;
  private boolean isCanceled = false;

  /**
   * Object constructor
   *
   * @param monitor - ProgressStatus, that shows the percents of the progress and
   *        the database that is synchronizing at the current moment
   * @param title - Text to display on popup area
   * @see GaugeField
   * @see PopupScreen
   */
  public ProgressBar(int  max, String title) {
//    this.monitor = monitor;
//    monitor.setMessage(title);

    FontFamily fontFamily[] = FontFamily.getFontFamilies();
    Font font = fontFamily[3].getFont(FontFamily.SCALABLE_FONT, 12);

    DialogFieldManager manager = new DialogFieldManager();
    popup = new PopupScreen(manager);
    label = new LabelField(title);
    gaugeField = new GaugeField("", 0, max, 0, GaugeField.PERCENT);
//    synchronized (UiApplication.getEventLock()) {
//      cancelButton = new CustomButtonField("Cancel", font, 30, 20) {
//        public boolean trackwheelClick(int status, int time) {
          //Dialog.alert("here");
//          isCanceled = true;
//          return true;
//        }
//      };
//    }
    manager.addCustomField(label);
    manager.addCustomField(gaugeField);
//    manager.addCustomField(cancelButton);
  }

  public void open() {
     UiApplication.getUiApplication().pushScreen(popup);
     popup.doPaint();
  }

//  public void setMax(int max) {
//    gaugeField = new GaugeField(null, 0, max, 0, GaugeField.PERCENT);
//  }

  public void update(int i) {
//    int percents = monitor.getPercents();
//    if (gaugeField.getValue() <= percents) {
//      label.setText(monitor.getMessage());
//      System.out.println("*****updating progress value="+i);
      gaugeField.setValue(i);
//    }
    popup.doPaint();
  }

  public void close() {
    UiApplication.getUiApplication().invokeLater(new Runnable() {
      public void run() {
        if (popup.isDisplayed()) {
          UiApplication.getUiApplication().popScreen(popup);
        }
      }
    });
  }

  public boolean isCanceled() {
    return isCanceled;
  }

  public void setCanceled(boolean isCanceled) {
    this.isCanceled = isCanceled;
  }

}
